extends Node


const SAVE_FILE_PATH: String = "user://save_data.json"


var save_data: Dictionary = { "highscore": 0 }


func write_save_data() -> void:
	var json_string: String = JSON.stringify(save_data)
	var save_file: FileAccess = FileAccess.open(SAVE_FILE_PATH, FileAccess.WRITE)
	if save_file == null:
		if OS.is_debug_build():
			print("Could not open save file({0}).".format([ SAVE_FILE_PATH ]))
		return
	save_file.store_string(json_string)
	save_file.close()


func read_save_data() -> void:
	var save_file: FileAccess = FileAccess.open(SAVE_FILE_PATH, FileAccess.READ)
	if save_file == null:
		if OS.is_debug_build():
			print("Could not open save file({0}).".format([ SAVE_FILE_PATH ]))
		return
	var json_string: String = save_file.get_as_text()
	if not json_string.is_empty():
		save_data = JSON.parse_string(json_string)
	save_file.close()
	
	
func _ready() -> void:
	read_save_data()
	
	
func save_highscore(new_highscore: int) -> void:
	save_data.highscore = new_highscore
	write_save_data()
